package com.seal.commons.utils;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;

import java.util.Collection;
import java.util.Map;
import java.util.function.Supplier;

/**
 * func 断言类
 * Date 2022/11/28 21:16<br/>
 *
 * @author yong.huang
 * @since JDK 1.8
 */
public abstract class Assert {

    public static <X extends Throwable> void notNull(Object object , Supplier<X> supplier) throws X {
        if(object == null){
            throw supplier.get();
        }
    }

    public static void notNull(Object object, String message) throws IllegalArgumentException {
        notNull(object, () -> new IllegalArgumentException(message));
    }

    public static <X extends Throwable> void notEmpty(Map<?,?> map, Supplier<X> supplier) throws X {
        if(MapUtils.isEmpty(map)){
            throw supplier.get();
        }
    }

    public static <X extends Throwable> void notEmpty(Map<?,?> map, String message) throws X {
       notEmpty(map, ()-> new IllegalArgumentException(message));
    }

    public static <X extends Throwable> void notEmpty(Collection<?> collection, Supplier<X> supplier) throws X {
        if(CollectionUtils.isEmpty(collection)){
            throw supplier.get();
        }
    }

    public static <X extends Throwable> void notEmpty(Collection<?> collection, String message) throws X {
        notEmpty(collection, ()-> new IllegalArgumentException(message));
    }

    public static <X extends Throwable> void notEmpty(Object[] array, Supplier<X> supplier) throws X {
        if(array == null || array.length == 0){
            throw supplier.get();
        }
    }

    public static <X extends Throwable> void notEmpty(Object[] array, String message) throws X {
        notEmpty(array, ()-> new IllegalArgumentException(message));
    }

    public static <X extends Throwable>void notBlank(String text, Supplier<X> supplier) throws X {
        if (StringUtils.isBlank(text)) {
            throw supplier.get();
        }
    }

    public static void notBlank(String text, String message) throws IllegalArgumentException {
        notBlank(text, () -> new IllegalArgumentException(message));
    }
}
